<template>
  <div class="card">
    <div class="card-content">
      <div class="card-top">
        <a href="javascript:;" @click="handleGoPage(path)"
          ><span>{{ title }}</span></a
        >
        <div class="card-direction" v-if="isSwiper">
          <span @click="handleLeft"></span>
          <span @click="handleRight"></span>
        </div>
        <div class="card-slot" v-else>
          <slot name="top-right"><a href="javascript:;" @click="handleGoPage(path)">更多<span class="iconfont icon-jiantouyou"></span></a></slot>
        </div>
      </div>
      <div class="content">
        <card-list
          :cardList="cardList"
          :num="num"
          :distance="distance"
          :isWrap="isWrap"
          :isSwiper="isSwiper"
        ></card-list>
      </div>
    </div>
  </div>
</template>

<script>
import CardList from "./cardlist/CardList.vue";
export default {
  components: { CardList },
  props: {
    title: String,
    path:String,
    isSwiper: Boolean,
    isWrap: {
      type: Boolean,
      default: false,
    },
    cardList: Array,
  },
  data() {
    return {
      num: 0,
      distance: 0,
      divide: 4,
    };
  },
  mounted() {
    window.addEventListener("resize", () => {
      if (window.innerWidth <= 992) {
        this.divide = 2;
      } else {
        this.divide = 4;
      }
    });
  },
  methods: {
    handleLeft() {
      if (this.num > 0) {
        this.num -= 2;
      } else {
        this.num = 0;
      }
      this.distance =
        (document.querySelector(".content").offsetWidth - 20) / this.divide;
    },
    handleRight() {
      if (this.num < this.cardList.length - this.divide) {
        this.num += 2;
      } else {
        this.num = this.cardList.length - this.divide;
      }
      this.distance =
        (document.querySelector(".content").offsetWidth - 20) / this.divide;
    },
    async handleGoPage(path){
      this.$router.push(path)
    }
  },
};
</script>

<style lang="less" scoped>
// @media only screen and (min-width: 1240px) {
//   .card-content {
//     width: 1200px;
//   }
// }
// @media only screen and (min-width: 992px) and (max-width: 1240px) {
//   .card-content {
//     width: 970px;
//   }
// }
// @media only screen and (max-width: 992px) {
//   .card-content {
//     width: 750px;
//   }
// }
// @media only screen and (max-width: 768px) {
//   .card-content {
//     width: 100%;
//   }
// }
.card {
  width: 100%;
  .card-content {
    margin: 0 auto;
    box-sizing: border-box;
    border: 10px solid #f5f5f5;
    background: #fff;
    overflow: hidden;
    .card-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 20px 20px 0;
      a {
        color: rgb(148, 148, 148);
        font-size: 14px;
        span {
          color: #000;
          font-size: 18px;
        }
        span.iconfont {
          color: rgb(148, 148, 148);
          font-size: 12px;
          margin-left: 5px;
        }
        &:hover {
          color: #3361ea;
          span {
            color: #3361ea;
          }
          span.iconfont {
            color: #3361ea;
            font-size: 12px;
            margin-left: 5px;
          }
        }
      }
      .card-direction {
        display: flex;
        span {
          width: 26px;
          height: 24px;
          line-height: 24px;
          border-radius: 5px;
          text-align: center;
          color: #ccc;
          border: 1px solid #ccc;
          position: relative;
          transition: all 0.2s;
          cursor: pointer;
          &::after {
            content: "";
            width: 8px;
            height: 8px;
            position: absolute;
            left: 10px;
            top: 8px;
            border-top: 1px solid #000;
            border-left: 1px solid #000;
            transform: rotate(-45deg);
          }
          &:nth-last-child(1) {
            margin-left: 10px;
            &::after {
              left: 6px;
              transform: rotate(135deg);
            }
          }
          &:hover {
            background: #3361ea;
            &::after {
              border-color: #fff;
            }
          }
        }
      }
      .card-slot{
        span{
          color:#ddd;
          margin: 0 10px;
        }
      }
    }
    .content {
      padding: 10px;
    }
  }
}
</style>